package com.bootdo.common.utils.excel;

import com.bootdo.common.utils.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import javax.servlet.ServletOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by HP on 2019/4/12.
 */
public class LiveExcelUtils {

    /**
     * 将直播的商品信息导入到excel文件中去
     * @param
     * @param out 输出表
     */
    public static int exportLiverExcel(List<Map<String,Object>> liveList,ServletOutputStream out)
    {
        try{
            //1.创建工作簿
            HSSFWorkbook workbook = new HSSFWorkbook();
            //1.1创建合并单元格对象
            CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,10);//起始行,结束行,起始列,结束列
            //1.2头标题样式
            HSSFCellStyle headStyle = createCellStyle(workbook,(short)16);
            //1.3列标题样式
            HSSFCellStyle colStyle = createCellStyle(workbook,(short)13);
            //1.4 内容样式
            /*HSSFCellStyle cellStyle=workbook.createCellStyle();
            cellStyle.setWrapText(true); //自动换行 */
            //2.创建工作表
            HSSFSheet sheet = workbook.createSheet("直播商品信息列表");
            //2.1加载合并单元格对象
            sheet.addMergedRegion(callRangeAddress);
            //设置默认列宽
            sheet.setDefaultColumnWidth(20);
            //设置列宽
           /* sheet.setColumnWidth(3, 10000);
            sheet.setColumnWidth(4, 10000);
            sheet.setColumnWidth(7, 9000);
            sheet.setColumnWidth(8, 10000);*/
            /*sheet.setColumnWidth(0, 17);
            sheet.setColumnWidth(1, 10);
            sheet.setColumnWidth(2, 20);
            sheet.setColumnWidth(3, 23);
            sheet.setColumnWidth(4, 20);
            sheet.setColumnWidth(5, 5);
            sheet.setColumnWidth(6, 7);
            sheet.setColumnWidth(7, 17);
            sheet.setColumnWidth(8, 25);
            sheet.setColumnWidth(9, 15);
            sheet.setColumnWidth(10, 20);*/


            //3.创建行
            //3.1创建头标题行;并且设置头标题
            HSSFRow row = sheet.createRow(0);
            HSSFCell cell = row.createCell(0);

            //加载单元格样式
            cell.setCellStyle(headStyle);
            cell.setCellValue("直播商品信息");

            //3.2创建列标题;并且设置列标题
            HSSFRow row2 = sheet.createRow(1);
            //String[] titles = {"订单号","待发货","金额","商品名","货品规格","SKU","数量","收件人姓名","收件人电话","收件人地址","买家留言"};
            String[] titles = {"商品","商品图","开始时间","结束时间","创建时间","修改时间","0默认1删除"};
            for(int i=0;i<titles.length;i++)
            {
                HSSFCell cell2 = row2.createCell(i);
                //加载单元格样式
                cell2.setCellStyle(colStyle);
                cell2.setCellValue(titles[i]);
            }
            //4.操作单元格;将用户列表写入excel
            if(liveList != null)
            {
                int size = 1;
                int j = 0;
                for(Map<String,Object> map : liveList) {
                   // List<Map<String,Object>> goodsList = (List<Map<String,Object>>)(map.get("goodsList"));
                   // size = goodsList.size();
                    //创建数据行,前面有两行,头标题行和列标题行
                    HSSFRow row3 = sheet.createRow(j+2);

                    HSSFCell cell0 = row3.createCell(0);
                    cell0.setCellValue(StringUtils.getString(map.get("goodsName")));

                    HSSFCell cell1 = row3.createCell(1);
                    cell1.setCellValue(StringUtils.getString(map.get("goodsImg")));

                    HSSFCell cell2 = row3.createCell(2);
                    Date starAt =new Date(map.get("starAt").toString());
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    cell2.setCellValue(sdf.format(starAt));

                    HSSFCell cell3 = row3.createCell(3);
                    Date endAt =new Date(map.get("endAt").toString());
                    cell3.setCellValue(sdf.format(endAt));

                    HSSFCell cell4 = row3.createCell(4);
                    Date caAt =new Date(map.get("caAt").toString());
                    cell4.setCellValue(sdf.format(caAt));

                    HSSFCell cell5 = row3.createCell(5);
                    cell5.setCellValue("");

                    HSSFCell cell6 = row3.createCell(6);
                    cell6.setCellValue(StringUtils.getString(map.get("delFlag")));

                    if (size > 1) {
                        //合并单元格
                        CellRangeAddress callRangeAddress0 = new CellRangeAddress(j+2,j+1+size,0,0);//起始行,结束行,起始列,结束列
                        sheet.addMergedRegion(callRangeAddress0);
                        CellRangeAddress callRangeAddress1 = new CellRangeAddress(j+2,j+1+size,1,1);//起始行,结束行,起始列,结束列
                        sheet.addMergedRegion(callRangeAddress1);
                        CellRangeAddress callRangeAddress2 = new CellRangeAddress(j+2,j+1+size,2,2);//起始行,结束行,起始列,结束列
                        sheet.addMergedRegion(callRangeAddress2);
                        CellRangeAddress callRangeAddress3 = new CellRangeAddress(j+2,j+1+size,3,3);//起始行,结束行,起始列,结束列
                        sheet.addMergedRegion(callRangeAddress3);
                        CellRangeAddress callRangeAddress4 = new CellRangeAddress(j+2,j+1+size,4,4);//起始行,结束行,起始列,结束列
                        sheet.addMergedRegion(callRangeAddress4);
                        CellRangeAddress callRangeAddress5 = new CellRangeAddress(j+2,j+1+size,5,5);//起始行,结束行,起始列,结束列
                        sheet.addMergedRegion(callRangeAddress5);
                        CellRangeAddress callRangeAddress6 = new CellRangeAddress(j+2,j+1+size,6,6);//起始行,结束行,起始列,结束列
                        sheet.addMergedRegion(callRangeAddress6);
                        CellRangeAddress callRangeAddress7 = new CellRangeAddress(j+2,j+1+size,7,7);//起始行,结束行,起始列,结束列
                        sheet.addMergedRegion(callRangeAddress7);
                        CellRangeAddress callRangeAddress8 = new CellRangeAddress(j+2,j+1+size,8,8);//起始行,结束行,起始列,结束列
                        sheet.addMergedRegion(callRangeAddress8);
                    }
                    j = j+size;
                }
            }
            //5.输出
            workbook.write(out);
            workbook.close();
            return 1;
            //out.close();
        }catch(Exception e)
        {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 将用户预约商品直播的信息导入到excel文件中去
     * @param
     * @param out 输出表
     */
    public static int exportAppointmentExcel(List<Map<String,Object>> AppointmentExcelList,ServletOutputStream out)
    {
        try{
            //1.创建工作簿
            HSSFWorkbook workbook = new HSSFWorkbook();
            //1.1创建合并单元格对象
            CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,10);//起始行,结束行,起始列,结束列
            //1.2头标题样式
            HSSFCellStyle headStyle = createCellStyle(workbook,(short)16);
            //1.3列标题样式
            HSSFCellStyle colStyle = createCellStyle(workbook,(short)13);
            //1.4 内容样式
            /*HSSFCellStyle cellStyle=workbook.createCellStyle();
            cellStyle.setWrapText(true); //自动换行 */
            //2.创建工作表
            HSSFSheet sheet = workbook.createSheet("预约信息列表");
            //2.1加载合并单元格对象
            sheet.addMergedRegion(callRangeAddress);
            //设置默认列宽
            sheet.setDefaultColumnWidth(20);
            //设置列宽
           /* sheet.setColumnWidth(3, 10000);
            sheet.setColumnWidth(4, 10000);
            sheet.setColumnWidth(7, 9000);
            sheet.setColumnWidth(8, 10000);*/
            /*sheet.setColumnWidth(0, 17);
            sheet.setColumnWidth(1, 10);
            sheet.setColumnWidth(2, 20);
            sheet.setColumnWidth(3, 23);
            sheet.setColumnWidth(4, 20);
            sheet.setColumnWidth(5, 5);
            sheet.setColumnWidth(6, 7);
            sheet.setColumnWidth(7, 17);
            sheet.setColumnWidth(8, 25);
            sheet.setColumnWidth(9, 15);
            sheet.setColumnWidth(10, 20);*/


            //3.创建行
            //3.1创建头标题行;并且设置头标题
            HSSFRow row = sheet.createRow(0);
            HSSFCell cell = row.createCell(0);

            //加载单元格样式
            cell.setCellStyle(headStyle);
            cell.setCellValue("预约信息");

            //3.2创建列标题;并且设置列标题
            HSSFRow row2 = sheet.createRow(1);
            //String[] titles = {"订单号","待发货","金额","商品名","货品规格","SKU","数量","收件人姓名","收件人电话","收件人地址","买家留言"};
            String[] titles = {"商品","预约用户","手机号","用户等级","创建时间"};
            for(int i=0;i<titles.length;i++)
            {
                HSSFCell cell2 = row2.createCell(i);
                //加载单元格样式
                cell2.setCellStyle(colStyle);
                cell2.setCellValue(titles[i]);
            }
            //4.操作单元格;将用户列表写入excel
            if(AppointmentExcelList != null)
            {
                int size = 1;
                int j = 0;
                for(Map<String,Object> map : AppointmentExcelList) {
                    // List<Map<String,Object>> goodsList = (List<Map<String,Object>>)(map.get("goodsList"));
                    // size = goodsList.size();
                    //创建数据行,前面有两行,头标题行和列标题行
                    HSSFRow row3 = sheet.createRow(j+2);

                    HSSFCell cell0 = row3.createCell(0);
                    cell0.setCellValue(StringUtils.getString(map.get("goodsName")));

                    HSSFCell cell1 = row3.createCell(1);
                    cell1.setCellValue(StringUtils.getString(map.get("userName")));

                    HSSFCell cell2 = row3.createCell(2);
                    cell2.setCellValue(StringUtils.getString(map.get("phone")));


                    HSSFCell cell3 = row3.createCell(3);
                    int level = Integer.valueOf(map.get("level").toString());
                    if (level == 0) {
                        cell3.setCellValue("普通用户");
                    } else if (level == 1) {
                        cell3.setCellValue("团长");
                    } else if (level == 2) {
                        cell3.setCellValue("核心");
                    } else if (level == 3) {
                        cell3.setCellValue("合伙人");
                    }

                    HSSFCell cell4 = row3.createCell(4);
                    cell4.setCellValue( map.get("ca_at").toString().replace(".0",""));

                    if (size > 1) {
                        //合并单元格
                        CellRangeAddress callRangeAddress0 = new CellRangeAddress(j+2,j+1+size,0,0);//起始行,结束行,起始列,结束列
                        sheet.addMergedRegion(callRangeAddress0);
                        CellRangeAddress callRangeAddress1 = new CellRangeAddress(j+2,j+1+size,1,1);//起始行,结束行,起始列,结束列
                        sheet.addMergedRegion(callRangeAddress1);
                        CellRangeAddress callRangeAddress2 = new CellRangeAddress(j+2,j+1+size,2,2);//起始行,结束行,起始列,结束列
                        sheet.addMergedRegion(callRangeAddress2);
                        CellRangeAddress callRangeAddress3 = new CellRangeAddress(j+2,j+1+size,3,3);//起始行,结束行,起始列,结束列
                        sheet.addMergedRegion(callRangeAddress3);
                        CellRangeAddress callRangeAddress4 = new CellRangeAddress(j+2,j+1+size,4,4);//起始行,结束行,起始列,结束列
                        sheet.addMergedRegion(callRangeAddress4);
                        CellRangeAddress callRangeAddress5 = new CellRangeAddress(j+2,j+1+size,5,5);//起始行,结束行,起始列,结束列
                        sheet.addMergedRegion(callRangeAddress5);
                        CellRangeAddress callRangeAddress6 = new CellRangeAddress(j+2,j+1+size,6,6);//起始行,结束行,起始列,结束列
                        sheet.addMergedRegion(callRangeAddress6);
                        CellRangeAddress callRangeAddress7 = new CellRangeAddress(j+2,j+1+size,7,7);//起始行,结束行,起始列,结束列
                        sheet.addMergedRegion(callRangeAddress7);
                        CellRangeAddress callRangeAddress8 = new CellRangeAddress(j+2,j+1+size,8,8);//起始行,结束行,起始列,结束列
                        sheet.addMergedRegion(callRangeAddress8);
                    }
                    j = j+size;
                }
            }
            //5.输出
            workbook.write(out);
            workbook.close();
            return 1;
            //out.close();
        }catch(Exception e)
        {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     *
     * @param workbook
     * @param fontsize
     * @return 单元格样式
     */
    private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook, short fontsize) {
        // TODO Auto-generated method stub
        HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
        //创建字体
        HSSFFont font = workbook.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        font.setFontHeightInPoints(fontsize);
        //加载字体
        style.setFont(font);
        return style;
    }




}
